/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CastleDefense.Managers;

import CastleDefense.Enemy;
import CastleDefense.Game;
import CastleDefense.units.PlacedUnit;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *
 * @author Firedemon
 */
public class EnemyManager 
{
    private Enemy enemy;
    private List<PlacedUnit> unitList = new ArrayList<PlacedUnit>();
    
    public EnemyManager(Enemy e)
    {
        this.enemy = e;
    }
    
    public void doEnemyLogic()
    {
        if(enemy.getName().equals("Dark Illusionist"))
        {           
            if(enemy.isDead())
            {                
                List<PlacedUnit> removeList = new ArrayList<PlacedUnit>();
                double distance;
                unitList.addAll(Game.getPlacedUnitList());
                
                for (PlacedUnit unit : unitList)
                {
                    distance = getDistance(enemy.getMidX(), enemy.getMidY(), unit.getMidX(), unit.getMidY());
                    unit.tempDist = distance;
                }
                
                for(PlacedUnit pu : unitList)
                    if(pu.getName().equals("Spikes") || pu.getName().equals("Death Spikes"))
                        removeList.add(pu);
                
                if(!removeList.isEmpty())
                    unitList.removeAll(removeList);
                
                Collections.sort(unitList, new PlacedUnitComparator());
                
                Game.fireAtUnit(unitList.get(0), enemy, "shadowball_proj.GIF");
                Game.fireAtUnit(unitList.get(1), enemy, "shadowball_proj.GIF");
            }
        }
    }
    
    private double getDistance(double x1, double y1, double x2, double y2)
    {
        Point2D p1, p2;
        double distance;

        p1 = new Point2D.Double(x1+21, y1+21);
        p2 = new Point2D.Double(x2+21, y2+21);

        distance = p1.distance(p2);

        return distance;
    }
}
